package project_package;
import java.util.ArrayList;
import java.util.Calendar;

public class Sales {
	
	int id;
	boolean isConfirmed = false;
	boolean isCash = true;
	boolean isFinished = false;
	Customer customer;
	Employee employee;
	long date;
	
	ArrayList<SalesLineItem> list = new ArrayList<SalesLineItem>();

	public Sales(int id, Customer customer, Employee employee)
	{
		this.id = id;
		this.customer = customer;
		this.employee = employee;
		this.date = System.currentTimeMillis()/1000; // time in unix time format
	}
	
	// our items
	public ArrayList<SalesLineItem> getProductList() 
	{
		return list;
	}
	
	// total number of items
	public int getTotalItems() 
	{
		return list.size();
	}
	
	// total price the customer would pay
	public double getTotal()
	{
		if (list.isEmpty()) {
			return 0;
		}
		else {
			double totalPrice = 0.0;
			for(SalesLineItem line : list ) {				// we make a loop through all the products on the list
				totalPrice += line.getLinePriceDiscount(); // we add the sum of the current line product price
			}												// to the totalPrice variable
			return totalPrice-(totalPrice*customer.getDiscount()); // if customer has discount, he uses it
		}
	}
	
	// gets id
	public int getID() 
	{
		return id;
	}
	
	// if product exists or not
	public boolean findItem(Product product)
	{
		boolean found = false;
		for (SalesLineItem items : list)
		{
			if (items.getProduct().equals(product))
				found = true;
		}
		return found;
	}
	
	// is it confirmed
	public boolean getIsConfirmed() 
	{
		return isConfirmed;
	}
	
	// is it cash or card
	public boolean getIsCash() 
	{
		return isCash;
	}
	
	// is it finished
	public boolean getIsFinished()
	{
		return isFinished;
	}
	
	// gets the employee
	public Employee getEmployee() 
	{
		return employee;
	}
	
	// gets the customer
	public Customer getCustomer() 
	{
		return customer;
	}
	
	// returns datestamp in unix format - used to see last month's sales
	public long getDateLong() 
	{
		return date;
	}
	
	// returns date in a d.m.Y h:i format
	public String getDate()
	{
		// method to transform unix timestamp to normal date
		// example: 1355531526 will return 15.12.2012 01:32
		Calendar newDate = Calendar.getInstance();
		newDate.setTimeInMillis(date*1000);
		String hour = (newDate.get(Calendar.HOUR_OF_DAY) < 10 ? ("0" + String.valueOf(newDate.get(Calendar.HOUR_OF_DAY))) : String.valueOf(newDate.get(Calendar.HOUR_OF_DAY)));
		String minute = (newDate.get(Calendar.MINUTE) < 10 ? ("0" + String.valueOf(newDate.get(Calendar.MINUTE))) : String.valueOf(newDate.get(Calendar.MINUTE)));
		
		String dateString = newDate.get(Calendar.DAY_OF_MONTH)+"."+newDate.get(Calendar.MONTH)+"."+newDate.get(Calendar.YEAR)+" "+hour+":"+minute;
		return dateString;		
	}
	
	// the invoice / sales list
	public void getSalesList() 
	{
		if (list.isEmpty()) {
			System.out.println("There isn't anything on the shopping list");
		}
		else {
			int i = 1;
			double totalPrice = 0.0;
			for(SalesLineItem line : list ) {
				System.out.println("#"+i+": Product: "+line.getProduct()+" -- Quantity:"+line.getQuantity()+" -- Price:"+line.getLinePriceDiscount());
				totalPrice += line.getLinePriceDiscount();
			}
			System.out.println("-----------------");
			System.out.println("Total products:" + i);
			System.out.println("Total price without discount:" + totalPrice);
			System.out.println("Total price with customer discount:" + (totalPrice-(totalPrice*customer.getDiscount())));
		}
	}
	
	// sets the id
	public void setID(int id)
	{
		this.id = id;
	}
	
	// sets if confirmed or not
	public void setIsConfirmed(boolean isConfirmed) 
	{
		this.isConfirmed = isConfirmed;
	}
	
	// sets if cash or debit card
	public void setIsCash(boolean isCash)
	{
		this.isCash = isCash;
	}
	
	// sets if finished (paid)
	public void setIsFinished(boolean isFinished)
	{
		this.isFinished = isFinished;
	}

	public int getTotalProducts() {
		if (list.isEmpty()) {
			return 0;
		}
		else {
			int totalProducts = 0;
			for(SalesLineItem line : list ) {				// we make a loop through all the products on the list
				totalProducts += line.getQuantity(); // we add the sum of the current line product price
			}												// to the totalPrice variable
			return totalProducts; // if customer has discount, he uses it
		}
	}
}